package homework;

import java.util.Stack;

//作业标题(1115)
//1.括号匹配问题
//
//作业内容
//1.括号匹配问题
//https://leetcode.cn/problems/valid-parentheses/description/
public class h1115 {
    class Solution {
        private Stack<Character> stack = new Stack<>();
        public boolean isValid(String s) {
            if(s == null) return true;
            for(int i = 0; i < s.length(); i++){
                switch (s.charAt(i)){
                    case '(' :
                    case '[' :
                    case '{' : stack.push(s.charAt(i)); break;
                    case ')' :
                    {
                        if(stack.empty()) return false;
                        switch (stack.pop()){
                            case '(' : break;
                            default: return false;
                        }
                        break;
                    }
                    case ']' :
                    {
                        if(stack.empty()) return false;
                        switch (stack.pop()){
                            case '[' : break;
                            default: return false;
                        }
                        break;
                    }
                    case '}' :
                    {
                        if(stack.empty()) return false;
                        switch (stack.pop()){
                            case '{' : break;
                            default: return false;
                        }
                        break;
                    }
                }
            }

            return stack.empty();
        }
    }
}
